package com.masterclient.app.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;

import android.util.AttributeSet;

import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.masterclient.app.R;
import com.eoner.baselibrary.utils.ScreenUtils;
import java.util.ArrayList;
import java.util.List;


public class AutoNextLineLinearlayout extends LinearLayout {

    private Context context;
    private int priceTextColor = 0xFF141922;
    private float priceTextSize = ScreenUtils.dp2px(17), unitTextSize = ScreenUtils.dp2px(12);

    private TextView integralTv,integralUnitTv,addTv,priceUnitTv,priceTv;
    private LinearLayout integralLayout,priceLayout;

    public AutoNextLineLinearlayout(Context context) {
        this(context,null);
    }

    public AutoNextLineLinearlayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        setOrientation(HORIZONTAL);
        setGravity(Gravity.TOP);

        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.PriceIntegral);
        priceTextColor = mTypedArray.getColor(R.styleable.PriceIntegral_priceTextColor, 0xFF141922);
        priceTextSize = mTypedArray.getDimension(R.styleable.PriceIntegral_priceTextSize, ScreenUtils.dp2px(17));
        unitTextSize = mTypedArray.getDimension(R.styleable.PriceIntegral_unitTextSize, ScreenUtils.dp2px(12));
        mTypedArray.recycle();

    }


    private List<WarpLine> mWarpLineGroup;
    private int heightSize;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int withMode = MeasureSpec.getMode(widthMeasureSpec);
        int withSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int with = 0;
        int height = 0;
        int childCount = getChildCount();
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        switch (withMode) {
            case MeasureSpec.EXACTLY:
                with = withSize;
                break;
            case MeasureSpec.AT_MOST:
                for (int i = 0; i < childCount; i++) {
                    if (getChildAt(i).getVisibility() == VISIBLE)
                    with += getChildAt(i).getMeasuredWidth();
                }
                with += getPaddingLeft() + getPaddingRight();
                with = with > withSize ? withSize : with;
                break;
            case MeasureSpec.UNSPECIFIED:
                for (int i = 0; i < childCount; i++) {
                    if (getChildAt(i).getVisibility() == VISIBLE)
                    with += getChildAt(i).getMeasuredWidth();
                }
                with += getPaddingLeft() + getPaddingRight();
                break;
            default:
                with = withSize;
                break;

        }

        WarpLine warpLine = new WarpLine();
        mWarpLineGroup = new ArrayList<WarpLine>();
        for (int i = 0; i < childCount; i++) {
            if (getChildAt(i).getVisibility() == VISIBLE) {
                if (warpLine.lineWidth + getChildAt(i).getMeasuredWidth() /*+ mType.horizontal_Space*/ > with) {
                    if (warpLine.lineView.size() == 0) {
                        warpLine.addView(getChildAt(i));
                        mWarpLineGroup.add(warpLine);
                        warpLine = new WarpLine();
                    } else {
                        mWarpLineGroup.add(warpLine);
                        warpLine = new WarpLine();
                        warpLine.addView(getChildAt(i));
                    }
                } else {
                    warpLine.addView(getChildAt(i));
                }
            }
        }

        if (warpLine.lineView.size() > 0 && !mWarpLineGroup.contains(warpLine)) {
            mWarpLineGroup.add(warpLine);
        }
        height = getPaddingTop() + getPaddingBottom();
        for (int i = 0; i < mWarpLineGroup.size(); i++) {
            height += mWarpLineGroup.get(i).height;
        }
        switch (heightMode) {
            case MeasureSpec.EXACTLY:
                height = heightSize;
                break;
            case MeasureSpec.AT_MOST:
                height = height > heightSize ? heightSize : height;
                break;
            case MeasureSpec.UNSPECIFIED:
                break;
            default:
                break;
        }
        setMeasuredDimension(with, height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //t = getPaddingTop();
        for (int i = 0; i < mWarpLineGroup.size(); i++) {
            int left = getPaddingLeft();
            WarpLine warpLine = mWarpLineGroup.get(i);
            int lastWidth = getMeasuredWidth() - warpLine.lineWidth;
            for (int j = 0; j < warpLine.lineView.size(); j++) {
                View view = warpLine.lineView.get(j);
                LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
                t = getPaddingTop();
                if(view.getId() == R.id.line_price_txt | view.getId() == R.id.earn_txt){
                    t = t - ScreenUtils.dp2px(3);
                }
                if(view.getId() == R.id.coupon_price_layout){
                    t = t - ScreenUtils.dp2px(2);
                }
                view.layout(left, t + warpLine.height- view.getMeasuredHeight() , left + view.getMeasuredWidth(), t + warpLine.height);
                left += view.getMeasuredWidth() + ScreenUtils.dp2px(3);
            }
            t += warpLine.height;
        }
    }

    private final class WarpLine {
        private List<View> lineView = new ArrayList<View>();
        private int lineWidth = getPaddingLeft() + getPaddingRight();
        private int height = 0;
        private void addView(View view) {
            height = height > view.getMeasuredHeight() ? height : view.getMeasuredHeight();
            lineWidth += view.getMeasuredWidth();
            lineView.add(view);
        }
    }


}
